<div class="layuimini-main">

    <div class="layui-form layuimini-form" lay-filter="editAgvForm">
        <div class="layui-form-item" hidden>
            <label class="layui-form-label required">机器人编号</label>
            <div class="layui-input-block">
                <input type="text" id="robotId" name="id" lay-verify="required" lay-reqtext="AGV名称不能为空" placeholder="请输入AGV名称" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">机器人名称</label>
            <div class="layui-input-block">
                <input type="text" name="name" lay-verify="required" lay-reqtext="AGV名称不能为空" placeholder="请输入AGV名称" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">机器人IP</label>
            <div class="layui-input-block">
                <input type="text" id="IP" name="ip" lay-verify="required|ip|repeat" lay-reqtext="IP地址不能为空" placeholder="请输入IP地址" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">机器人Port</label>
            <div class="layui-input-block">
                <input type="number" name="port" placeholder="请输入AGV_Port" value="7273" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">所在区域</label>
            <div class="layui-input-block">
                <input type="text" name="region" placeholder="请输入所在区域" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">可背负物料</label>
            <div class="layui-input-block">
                <input type="text" name="payloads" placeholder="请输入可背负的物料型号" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">所在楼层</label>
            <div class="layui-input-block">
                <input type="number" name="floor" placeholder="请输入所在楼层" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
            </div>
        </div>
    </div>
</div>
<script>
    layui.use(['form', 'table'], function () {
        var form = layui.form,
            layer = layui.layer,
            table = layui.table,
            $ = layui.$;

        form.verify({
            ip: [
                /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/,
                'IP地址不符合规则'
            ],
            repeat: function (value, item) { //value：表单的值、item：表单的DOM对象
                var ip = $("#IP").val();
                var checkResult = "";
                $.ajax({
                    url: "api/Robot/Check/ip=" + ip,
                    type: "GET",
                    async: false,
                    success: function (result) {
                        if (result.data>1) {
                            checkResult = "该IP已经被占用!";
                        }
                    },
                    error: function () {
                    }
                });
                return checkResult;
            },
        });

        //监听提交
        form.on('submit(saveBtn)', function (data) {

             var robotId= $("#robotId").val();
            $.ajax({
                url: 'api/Robot/Update/' + robotId,
                method: 'put',
                contentType: 'application/json',
                cache: false,
                data: JSON.stringify(data.field),
                success: function (res) {
                    if (res.code === 200) {
                        var index = layer.msg('修改成功!', { icon:1 }, function () {
                            {
                                layer.closeAll();
                            }
                        });

                    }
                    else
                        layer.msg('修改失败!' + res.msg, { icon: 5 });
                },
                error: function (data) {

                    layer.msg('修改失败!', {icon:5});

                }
            });

            return false;
        });

    });
</script>